## Project: Autocracy UPR project ----------------------------------------------
##        04 Explanatory Data Analysis -----------------------------------------
## Author:
##   - Chun-Young Park (UGA)
##   - Sang-Hoon Park (UofSC)
##
## Updated: Aug 30 2024
## R version 4.4.0 (2024-04-24)
## Platform: aarch64-apple-darwin20
## Running under: macOS Sonoma 14.6.1

if (!require("devtools")) install.packages("devtools")
if (!require("pacman")) install.packages("pacman")
if (!require("rqog")) remotes::install_github("ropengov/rqog")
pacman::p_load(rqog, rio, tidyverse, dendextend, splitstackshape, psych, dlookr, tidyverse)
if (!require("dendextendRcpp")) devtools::install_github('talgalili/dendextendRcpp')
if (!require("vdemdata")) devtools::install_github("vdeminstitute/vdemdata")
if (!require("ggrepel")) install.packages("ggrepel")
if (!require("patchwork")) install.packages("patchwork")
if (!require("futurevisions")) devtools::install_github("JoeyStanley/futurevisions")

dlookr::import_google_font("Barlow Semi Condensed")

theme_clean <- function() {
  theme_minimal(base_family = "Barlow Semi Condensed") +
    theme(panel.grid.minor = element_blank(),
          plot.title = element_text(face = "bold", color = "black"),
          plot.subtitle = element_text(face = "bold", color = "black"),
          axis.title = element_text(family = "Barlow Semi Condensed Medium", 
                                    color = "black", size = rel(1.5)),
          axis.text = element_text(color = "black", size = rel(1.3)),
          strip.text = element_text(family = "Barlow Semi Condensed",
                                    face = "bold", size = rel(1), hjust = 0,
                                    color = "black"),
          strip.background = element_rect(fill = "white", color = NA),
          plot.caption = element_text(hjust = 0, color = "black"),
          legend.position = "bottom")
}
showtext::showtext_auto(T)
ggplot2::theme_set(theme_clean())

library(tidyverse)

### Import analysis data -------------------------------------------------------
upr_vdem_hrs <- readRDS("data/data for analysis/upr_vdem_hrs.RDS")

#### Table 1. The example of the UPR recommendation ------------------------------------------------
upr_vdem_hrs |> mutate(year = as.numeric(as.character(year))) |> 
  dplyr::filter(year > 2007) |>
  drop_na(regime_dyads_lexi, severity) |>
  group_by(regime_dyads_lexi) |> 
  summarise(count = n()) |> ungroup() |>
  mutate(total = sum(count),
         ratio = count/total) -> table

save(table, file = "tables/table1.RData")

table |> kableExtra::kbl("latex", 
                         booktabs = T, format.args = list(big.mark = ","),
                         longtable = F,
                         digits = 3) |> 
  kableExtra::kable_paper(font_size = 9) |> 
  kableExtra::save_kable(format = "latex",
                         "tables/table1.tex")

#### Table 2. Distribution of regime dyads in the sample -------------------------------------------
example <- tibble(
  Reviewer = c("Belgium", "Bulgaria",  "Kuwait"),
  Recommendation = c("Continue to reduce the offences punishable by the death penalty.",
                     "Make further progress in the reduction of the scope of capital offences in Chinese legislation.",
                     "Increase the number of institutions providing professional services for the upbringing and protection of orphans, protection of homeless children and the rehabilitation of children with disabilities."),
  Raw = c(2, 4, 5),
  Ordinal = c("1 (Not shaming)", "2 (Neutral)", "3 (Shaming)"))

example |> kableExtra::kbl("latex", 
                           booktabs = T, format.args = list(big.mark = ","),
                           digits = 3,
                           align = c("l", "l", "r", "r")) |> 
  kableExtra::add_header_above(header = c(" " = 2, "UPR Categorization" = 2)) |> 
  kableExtra::column_spec(1, width = "1.5cm") |> 
  kableExtra::column_spec(2, width = "7.5cm") |> 
  kableExtra::column_spec(4, width = "5cm") |> 
  kableExtra::kable_paper(font_size = 8) |> 
  kableExtra::save_kable(format = "latex",
                         "tables/table2.tex")

### Recommendation severity by different dyads -----------------------------------------------------
#### Figure 1. The proportion of recommendation severity by different dyads (Reviewer-SuR) ---------
upr_vdem_hrs |> drop_na(severity) |>
  drop_na(regime_dyads_lexi) |>
  group_by(regime_dyads_lexi, severity) |> 
  dplyr::select(7) |> summarize(total = n()) |> ungroup() |>
  group_by(regime_dyads_lexi) |> 
  mutate(regime_dyads_lexi = 
           factor(regime_dyads_lexi,
                  levels = c("Democracy Dyads", "Autocracy Dyads",
                             "Auto(REV)-Demo(SUR) Mixed Dyads","Demo(REV)-Auto(SUR) Mixed Dyads"), 
                  label =  c("Democracy Dyads", "Autocracy Dyads",
                             "Auto(REV)-Demo(SUR) Mixed Dyads","Demo(REV)-Auto(SUR) Mixed Dyads"))) |> 
  mutate(sum = sum(total, na.rm = T),
         ratio = total/sum,
         severity.fa = case_when(
           severity == 1L ~ "Praising",
           severity == 2L ~ "Neutral",
           severity == 3L ~ "Shaming",
           T ~ NA_character_),
         severity.fa = factor(severity.fa, 
                              levels = c(
                                "Praising", "Neutral", "Shaming"))) |>
  ggplot(aes(x = regime_dyads_lexi,
             shape = severity.fa,
             fill = severity.fa, y = ratio)) + 
  geom_bar(stat = "identity", position=position_dodge(0.9)) + 
  labs(x = "", y = "Percent",
       caption = str_wrap("Note: The y-axis represents the percentage of comments within each dyad type, and the x-axis categorizes the different dyad types. The color-coded bars show the proportion of comments classified as Praising (blue), Neutral (yellow), and Shaming (red). 
                          In the mixed dyads, the first label indicates the regime type of the reviewer, while the second label denotes the regime type of the state under review. For example, 'Auto-Demo Mixed Dyads' means that an autocracy is reviewing a democracy.", 
                          125, exdent = 10)) +
  scale_fill_manual(values = c(futurevisions::futurevisions("mars")[3],
                               futurevisions::futurevisions("mars")[2],
                               futurevisions::futurevisions("mars")[1])) + 
  scale_y_continuous(labels = scales::percent_format()) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) +
  theme(legend.title = element_blank(),
        legend.text = element_text(size = rel(1.3)),
        legend.margin=margin(0,0,0,0),
        legend.box.margin=margin(0, 0, 20, 0),
        plot.caption = element_text(size = rel(1.3)))

ggsave("figures/fig1.pdf", width = 12, height = 7, dpi = "retina")

